<?php
App::import("Vendor", "Smsgh/Api");

/**
 * CakePHP SmsTask
 * @author Francis Adu-Gyamfi <francis.adu-gyamfi@matrixdesigns.org>
 */
class SmsTask extends Shell {

    private $api = null;
    private $account = null;
    
    public $uses = array();

    public function __construct($stdout = null, $stderr = null, $stdin = null) {
        parent::__construct($stdout, $stderr, $stdin);
        
        $auth = new BasicAuth( Configure::read('smsgh.clientId'), Configure::read('smsgh.secret'));
        $host = new ApiHost($auth);
        $this->api = new MessagingApi($host);
        $this->account = new AccountApi($host);
    }
    
    public function execute() {
        
    }
    
    public function send($to = null, $text = null, $sender_id = 'Memberz') {
     
        if( $to == null ) {
            $to = "+233277593003";
        }
        
        if( $text == null ) {
            $text = "Hello there. This is a test message from memberz.org";
        }
        
        try {
            $message = new Message();
            $message->setFrom($sender_id);
            $message->setTo($to);
            $message->setContent($text);
            $message->setRegisteredDelivery(true);
            
            $messageResponse = $this->api->sendMessage($message);

            if ($messageResponse instanceof MessageResponse) {
                return array(
                    'status' => $messageResponse->getStatus(), 
                    'messageId' => $messageResponse->getMessageId()
                );
            } elseif ($messageResponse instanceof HttpResponse) {
                $status = $messageResponse->getStatus();
                
                if( $status == 0 ) {
                    return array(
                        'status' => $messageResponse->getStatus(), 
                        'messageId' => $messageResponse->getMessageId()
                    );
                }
                
                if( substr($status, 0, 1) != '2' ) {
                    $body = json_decode($messageResponse->getBody(), true);
                    $subcode = isset($body['Status']) ? $body['Status'] : '';
                    $code = $status;
                    
                    return array(
                        'status' => 'error',
                        'code' => $code,
                        'subcode' => $subcode,
                        'reason' => $this->getErrorCodeReason($code, $subcode)
                    ); 
                }
                
            }
            
        } catch (Exception $ex) {
            return array('error' => $ex->getTraceAsString());
        }
        
    }
    
    public function getErrorCodeReason($code, $subcode) {
        
        switch($code) {
            case '200':
                return 'The request was successfully handled by the API.';
            
            case '201':
                switch($subcode) {
                    case '0':
                        return 'The request was successful, and the messages has been sent for onward delivery.';                                       
                }
                break;
            
            case '400':
                switch($subcode) {
                    case '100':
                        return 'General invalid request. Returned when no data is sent or a malformed request is received.';     
                    case '1':
                        return 'Invalid Destination address received. The phone number recipient is not a valid phone number.';
                    case '2':
                        return 'Invalid Source Address was sent. You need to be aware of the sender address restrictions.';
                    case '3':
                        return 'The message body was too long.';
                }
                break;
        }
    }

}
